package com.layblog.config;

import com.layblog.common.lang.LayConstant;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Properties;

/**
 * 1.配置google验证码的生成规则 2.配置静态资源的映射
 *
 * @Author 林必昭
 * @Date 2021/7/31 14:45
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    LayConstant layConstant;

    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");                      //图片边框，yes/no
        properties.put("kaptcha.image.height", "38");                //图片高度，默认值50
        properties.put("kaptcha.image.width", "150");                //图片宽度 ，默认值200
        properties.put("kaptcha.textproducer.font.color", "black");  //字体颜色，合法值： r,g,b 或者 white,black,blue.	black
        properties.put("kaptcha.textproducer.font.size", "32");      //字体大小
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/avatar/**")
                .addResourceLocations("file:///" + layConstant.getUploadDir() + "/avatar/");
    }
}
